﻿@page
@model FytSoa.Web.Pages.FytAdmin.Sys.RoleModel
@{
ViewData["Title"] = "角色管理";
Layout = AdminLayout.Pjax(HttpContext);
}
<div id="container">
    <div class="layui-col-220">
        <div class="layui-nav-title">组织机构</div>
        <div id="tree" class="layui-tree-cus" style="overflow:auto;"></div>
    </div>
    <div class="right-col-body">
        <div class="list-wall">
            <div class="layui-form list-search">
                <button type="button" class="layui-btn layui-btn-sm" data-type="roleGroup"><i
                        class="layui-icon layui-icon-add-circle"></i> 添加角色组</button>
                <button type="button" class="layui-btn layui-btn-sm" data-type="organizeAdd"><i
                        class="layui-icon layui-icon-add-circle"></i> 添加角色</button>
                <button type="button" class="layui-btn layui-btn-sm" data-type="organizeDel"><i class="layui-icon"></i>
                    删除</button>
            </div>
            <table class="layui-hide" id="tablist" lay-filter="tool"></table>
        </div>
    </div>
    <script>layui.config({
            base: '/themes/js/modules/'
        }).use(['table', 'layer', 'jquery', 'tree', 'common'],
            function () {
                var table = layui.table,
                    layer = layui.layer,
                    $ = layui.jquery,
                    ztree = layui.ztree,
                    os = layui.common,
                    form = layui.form,
                    tree = layui.tree;
                $("#tree").css({ 'height': $(window).height() - 140 });
                table.render({
                    elem: '#tablist',
                    headers: os.getToken(),
                    url: '/api/role/getpages',
                    cols: [
                        [
                            { type: 'checkbox', fixed: 'left' },
                            {
                                field: 'name', title: '角色名称', sort: true, fixed: 'left', templet: function (d) {
                                    if (d.level == 0) {
                                        return d.name + '<span class="layui-badge layui-bg-cyan">组</span>';
                                    } else {
                                        return d.name;
                                    }
                                }
                            },
                            {
                                field: 'isSystem', title: '是否系统管理员', templet: function (d) {
                                    if (d.isSystem) {
                                        return '<span class="layui-badge-dot layui-bg-blue"></span>';
                                    } else {
                                        return '<span class="layui-badge-dot layui-bg-gray"></span>';
                                    }
                                }
                            },
                            { field: 'sort', title: '排序' },
                            { field: 'summary', title: '描述' },
                            { width: 100, title: '操作', templet: '#tool' }
                        ]
                    ],
                    page: false,
                    id: 'tables',
                    height: $(window).height() - 150,
                    where: {
                        limit: 1000
                    }
                });

                var guid = '', active = {
                    initTree() {
                        os.ajax('api/organize/gettree/', {}, function (res) {
                            tree.render({
                                elem: '#tree'
                                , data: res
                                , onlyIconControl: true
                                , click: function (obj) {
                                    guid = obj.data.id;
                                    active.reload();
                                }
                            });
                        });
                    },
                    reload: function () {
                        table.reload('tables',
                            {
                                page: false,
                                where: {
                                    key: guid,
                                    limit: 1000
                                }
                            });
                    },
                    roleGroup: function () {
                        os.Open('添加角色组', '/fytadmin/sys/rolegroup', '520px', '300px', function () {
                            active.reload();
                        });
                    },
                    organizeAdd: function () {
                        os.Open('添加角色', '/fytadmin/sys/rolemodify', '650px', '480px', function () {
                            active.reload();
                        });
                    },
                    organizeDel: function () {
                        var checkStatus = table.checkStatus('tables')
                            , data = checkStatus.data;
                        if (data.length === 0) {
                            os.error("请选择要删除的项目~");
                            return;
                        }
                        var str = '';
                        $.each(data, function (i, item) {
                            if (!item.isSystem) {
                                str += item.guid + ",";
                            }
                        });
                        if (str == '') {
                            os.error("请选择要删除的项目,超管状态不能删除~");
                            return;
                        }
                        layer.confirm('确定要执行批量删除吗？', function (index) {
                            layer.close(index);
                            var loadindex = layer.load(1, {
                                shade: [0.1, '#000']
                            });
                            os.ajax('api/role/delete/', { parm: str }, function (res) {
                                layer.close(loadindex);
                                if (res.statusCode === 200) {
                                    active.reload();
                                    os.success('删除成功！');
                                } else {
                                    os.error(res.message);
                                }
                            });
                        });

                    }
                };
                active.initTree();
                $('.list-search .layui-btn').on('click', function () {
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });

                //监听工具条
                table.on('tool(tool)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'edit') {
                        if (data.level == 1) {
                            os.Open('编辑角色', '/fytadmin/sys/rolemodify/?guid=' + data.guid, '650px', '480px', function () {
                                active.reload();
                            })
                        }
                        else {
                            os.Open('修改角色组', '/fytadmin/sys/rolegroup?guid=' + data.guid, '520px', '300px', function () {
                                active.reload();
                            });
                        }
                    }
                });
            });</script>
    <script type="text/html" id="tool">
        {{#  if(!d.isSystem){ }}
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit"><i class="layui-icon"></i> 修改</a>
        {{#  } }} 
    </script>
</div>